package p_stack_queue;

import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description: 用栈实现队列
 * User: 王圆豪
 * Date: 2024-08-19
 * Time: 0:48
 */
public class S_queue {
    Stack<Integer> stack1;
    Stack<Integer> stack2;

    public S_queue() {
        stack1 = new Stack<>();
        stack2 = new Stack<>();
    }

    public void push(int val) {
        stack1.push(val);
    }

    public int pop() {
        if (stack2.isEmpty()) {
            while (!stack1.isEmpty()) {
                stack2.push(stack1.pop());
            }
        }
        return stack2.pop();
    }

    public int peek() {
        if (stack2.isEmpty()) {
            while (!stack1.isEmpty()) {
                stack2.push(stack1.pop());
            }
        }
        return stack2.peek();
    }

    public boolean isEmpty() {
        return stack1.isEmpty() && stack2.isEmpty();
    }

    public static void main(String[] args) {
        S_queue s_queue = new S_queue();
        s_queue.push(4);
        System.out.println(s_queue.pop());
    }
}
